
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>升级 Django 到最新的版本 &#8212; Django 3.2.11.dev 文档</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="发送错误" href="error-reporting.html" />
    <link rel="prev" title="部署清单" href="deployment/checklist.html" />



 
<script src="../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);(function($) {
    $(document).ready(function() {
        $(".c-tab-unix").on("click", function() {
            $("section.c-content-unix").show();
            $("section.c-content-win").hide();
            $(".c-tab-unix").prop("checked", true);
        });
        $(".c-tab-win").on("click", function() {
            $("section.c-content-win").show();
            $("section.c-content-unix").hide();
            $(".c-tab-win").prop("checked", true);
        });
    });
})(jQuery);</script>
<link rel="stylesheet" href="../_static/console-tabs.css" type="text/css" />
  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 3.2.11.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="deployment/checklist.html" title="部署清单">previous</a>
     |
    <a href="index.html" title="操作指南" accesskey="U">up</a>
   |
    <a href="error-reporting.html" title="发送错误">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="howto-upgrade-version">
            
  <div class="section" id="s-upgrading-django-to-a-newer-version">
<span id="upgrading-django-to-a-newer-version"></span><h1>升级 Django 到最新的版本<a class="headerlink" href="#upgrading-django-to-a-newer-version" title="永久链接至标题">¶</a></h1>
<p>虽然有时是个复杂的过程，将 Django 升级到最新版有以下好处：</p>
<ul class="simple">
<li>新功能和优化</li>
<li>已修复的 bug。</li>
<li>旧版 Django 最终将不再收到安全更新。（参考 <a class="reference internal" href="../internals/release-process.html#supported-versions-policy"><span class="std std-ref">支持的版本</span></a>）</li>
<li>随着每个新 Django 发行版的发布而升级，可以使您的代码库保持最新，从而减少将来升级带来的痛苦。</li>
</ul>
<p>有些事情需要你考虑，这有助于使升级流程尽可能顺滑。</p>
<div class="section" id="s-required-reading">
<span id="required-reading"></span><h2>必读内容<a class="headerlink" href="#required-reading" title="永久链接至标题">¶</a></h2>
<p>若这是你第一次进行升级操作，那么阅读 <a class="reference internal" href="../internals/release-process.html"><span class="doc">不同发行进程的指南</span></a> 就非常有用。</p>
<p>随后，你要自主熟悉 Django 新版本的修改：</p>
<ul class="simple">
<li>阅读当前版本之后的每个“最终”版本的 <a class="reference internal" href="../releases/index.html"><span class="doc">发行说明</span></a> ，包括计划升级的版本。</li>
<li>阅读 <a class="reference internal" href="../internals/deprecation.html"><span class="doc">过期时间表</span></a> 获取相关版本信息。</li>
</ul>
<p>特别注意向前兼容修改，清楚了解成功的升级需要做什么。</p>
<p>若你更新的版本跨度超过一个特性版本（例如从 2.0 更新到 2.2），通常增量更新（从 2.0 到 2.1 再到 2.2）每个版本会比一次性更新更容易。对于每个发布特性，使用最新的补丁版本（例如，对于 2.1，使用 2.1.15）。</p>
<p>从一个长期支持（LTS）版本更新至下一个长期更新版本时，特别推荐同样的增量更新方法。</p>
</div>
<div class="section" id="s-dependencies">
<span id="dependencies"></span><h2>依赖<a class="headerlink" href="#dependencies" title="永久链接至标题">¶</a></h2>
<p>大多数情况下，将 Django 的依赖升至最新版本是必要的。若 Django 版本是最近发布的，但某些依赖没有很好适配，这些依赖可能无法支持最新版的 Django。这种情况下，你只能等新版本的依赖发布。</p>
</div>
<div class="section" id="s-resolving-deprecation-warnings">
<span id="resolving-deprecation-warnings"></span><h2>处理过期警告<a class="headerlink" href="#resolving-deprecation-warnings" title="永久链接至标题">¶</a></h2>
<p>在升级前，将使用当前 Django 版本引发的过期警告解决掉是个不错的注意。在升级前修复这些警告能确保你了解代码中哪些部分需要修改。</p>
<p>在 Python 中，过期警告默认是静默的。你必须用 Python 的命令行选项 <code class="docutils literal notranslate"><span class="pre">-Wa</span></code> 或 <span class="target" id="index-2"></span><a class="reference external" href="https://docs.python.org/3/using/cmdline.html#envvar-PYTHONWARNINGS" title="(在 Python v3.10)"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONWARNINGS</span></code></a> 环境变量将其打开。例如，在运行测试时显示警告：</p>
<div class="console-block" id="console-block-0">
<input class="c-tab-unix" id="c-tab-0-unix" type="radio" name="console-0" checked>
<label for="c-tab-0-unix" title="Linux/macOS">&#xf17c/&#xf179</label>
<input class="c-tab-win" id="c-tab-0-win" type="radio" name="console-0">
<label for="c-tab-0-win" title="Windows">&#xf17a</label>
<section class="c-content-unix" id="c-content-0-unix">
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python -Wa manage.py <span class="nb">test</span>
</pre></div>
</div>
</section>
<section class="c-content-win" id="c-content-0-win">
<div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> py -Wa manage.py test
</pre></div>
</section>
</div>
<p>假如你没有使用 Django 的 test runner，你可能还需要确认没有终端输出被截胡，这可能会隐藏过期警告。例如，若你使用 <a class="reference external" href="https://pytest.org/">pytest</a>:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span><span class="nv">PYTHONWARNINGS</span><span class="o">=</span>always pytest tests --capture<span class="o">=</span>no
</pre></div>
</div>
<p>在继续升级流程前处理当前 Django 版本报告的所有过期警告。</p>
<p>第三方应用可能会出于兼容多版本 Django 目的使用过期 APIs，所以你安装的应用报告的过期警告可能并不是问题。若某个包不支持最新版的 Django，考虑为其创建一个 issue 或发起一个 pull request。</p>
</div>
<div class="section" id="s-installation">
<span id="installation"></span><h2>安装<a class="headerlink" href="#installation" title="永久链接至标题">¶</a></h2>
<p>准备好后，就是 :doc:<a href="#id1"><span class="problematic" id="id2">`</span></a>安装新版 Django ` 的时候。若你正使用 :mod:<a href="#id3"><span class="problematic" id="id4">`</span></a>virtual environment <a href="#id5"><span class="problematic" id="id6">`</span></a>，且这是一次关键性升级，你可能想要先配置一个包含所有依赖的环境。</p>
<p>若你用 <a class="reference external" href="https://pip.pypa.io/">pip</a> 安装 Django，你可以使用 <code class="docutils literal notranslate"><span class="pre">--upgrade</span></code> 或 <code class="docutils literal notranslate"><span class="pre">-U</span></code> 标志：</p>
<div class="console-block" id="console-block-1">
<input class="c-tab-unix" id="c-tab-1-unix" type="radio" name="console-1" checked>
<label for="c-tab-1-unix" title="Linux/macOS">&#xf17c/&#xf179</label>
<input class="c-tab-win" id="c-tab-1-win" type="radio" name="console-1">
<label for="c-tab-1-win" title="Windows">&#xf17a</label>
<section class="c-content-unix" id="c-content-1-unix">
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python -m pip install -U Django
</pre></div>
</div>
</section>
<section class="c-content-win" id="c-content-1-win">
<div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> py -m pip install -U Django
</pre></div>
</section>
</div>
</div>
<div class="section" id="s-testing">
<span id="testing"></span><h2>测试<a class="headerlink" href="#testing" title="永久链接至标题">¶</a></h2>
<p>当环境准备好后，先为应用 <a class="reference internal" href="../topics/testing/overview.html"><span class="doc">运行完整的测试套件</span></a>。再说一次，将过期警告开关打开很有用，这样就能在测试输出中看到过期警告（若你用 <code class="docutils literal notranslate"><span class="pre">manage.py</span> <span class="pre">runserver</span></code> 手动测试，也能有该标志）：</p>
<div class="console-block" id="console-block-2">
<input class="c-tab-unix" id="c-tab-2-unix" type="radio" name="console-2" checked>
<label for="c-tab-2-unix" title="Linux/macOS">&#xf17c/&#xf179</label>
<input class="c-tab-win" id="c-tab-2-win" type="radio" name="console-2">
<label for="c-tab-2-win" title="Windows">&#xf17a</label>
<section class="c-content-unix" id="c-content-2-unix">
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python -Wa manage.py <span class="nb">test</span>
</pre></div>
</div>
</section>
<section class="c-content-win" id="c-content-2-win">
<div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> py -Wa manage.py test
</pre></div>
</section>
</div>
<p>在你运行测试，并修复所有问题后。由于你刚看了发布说明，可能现在也是重构代码，消除所有过期警告，尝尝 Django 最新功能特性的好时机。</p>
</div>
<div class="section" id="s-deployment">
<span id="deployment"></span><h2>部署<a class="headerlink" href="#deployment" title="永久链接至标题">¶</a></h2>
<p>当你十分确信你的应用能兼容新版 Django 时，你就准备好去往下一步， <a class="reference internal" href="deployment/index.html"><span class="doc">部署</span></a> 升级后的 Django 工程。</p>
<p>若你使用 Django 提供的缓存，你需要考虑在升级后清空缓存。否则，你可能会卷入麻烦中，举个例子，若你缓存了 pickled 对象，而这些对象并不能确保跨版本 pickle 兼容。一个过期兼容性例子是缓存 pickled <a class="reference internal" href="../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> 对象，不论是直接或间接由 <a class="reference internal" href="../topics/cache.html#django.views.decorators.cache.cache_page" title="django.views.decorators.cache.cache_page"><code class="xref py py-func docutils literal notranslate"><span class="pre">cache_page()</span></code></a> 装饰器创建。</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">升级 Django 到最新的版本</a><ul>
<li><a class="reference internal" href="#required-reading">必读内容</a></li>
<li><a class="reference internal" href="#dependencies">依赖</a></li>
<li><a class="reference internal" href="#resolving-deprecation-warnings">处理过期警告</a></li>
<li><a class="reference internal" href="#installation">安装</a></li>
<li><a class="reference internal" href="#testing">测试</a></li>
<li><a class="reference internal" href="#deployment">部署</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="deployment/checklist.html"
                        title="上一章">部署清单</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="error-reporting.html"
                        title="下一章">发送错误</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/howto/upgrade-version.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">12月 07, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="deployment/checklist.html" title="部署清单">previous</a>
     |
    <a href="index.html" title="操作指南" accesskey="U">up</a>
   |
    <a href="error-reporting.html" title="发送错误">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>